package com.weic.easycache.framework.theme;

import java.lang.ref.WeakReference;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;

import com.weic.easycache.Debug;

import android.app.Activity;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Typeface;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class ThemePlugin {

	public static void applyThemeDefault() {
		currentTheme = Theme.DEFAULT;
		applyTheme(currentTheme);
	}

	public static void applyThemeCurrent() {
		applyTheme(currentTheme);
	}

	public static void applyTheme(Theme theme) {
		currentTheme = theme;
		//TODO

	}

	/**应用默认主题
	 * @param context	当前Activity创建的时候，只在本Activity范围内应用主题**/
	public static void applyThemeDefault(Context context) {
		currentTheme = Theme.DEFAULT;
		applyTheme(currentTheme, context);
	}

	/**应用当前主题（通常使用这个）
	 * @param context	当前Activity创建的时候，只在本Activity范围内应用主题**/
	public static void applyThemeCurrent(Context context) {
		applyTheme(currentTheme, context);
	}

	/**应用参数对应的主题
	 * @param context	当前Activity创建的时候，只在本Activity范围内应用主题**/
	public static void applyTheme(Theme theme, Context context) {
		currentTheme = theme;
		currentContext = (Activity)context;
		//TODO

	}

	private static boolean applyViewParam(View view, ThemeParamInfo info, Resources res) {
		try {
			int formattedResID = currentTheme.formatResID(info.resName, res);
			ColorStateList colors = null;
			int color = 0;

			switch(info.param) {
			case BG:

				break;
			case BGCOLOR:

				break;
			case TEXTSIZE:

				break;
			case TEXTTYPEFACE:
				Typeface tf = null;
				if(info.) {
					switch (typefaceIndex) {
					case SANS:
						tf = Typeface.SANS_SERIF;
						break;
					case SERIF:
						tf = Typeface.SERIF;
						break;
					case MONOSPACE:
						tf = Typeface.MONOSPACE;
						break;
					}
				}
				setTypeface(tf, styleIndex);
				((TextView)view).setTypeface(tf, styleIndex);
				break;
			case TEXTCOLOR:
				colors = currentTheme.inner ? res.getColorStateList(formattedResID) : getColorStateListWithExtTheme();
				((TextView)view).setTextColor(colors);
				break;
			case TEXTCOLORHINT:
				colors = currentTheme.inner ? res.getColorStateList(formattedResID) : getColorStateListWithExtTheme();
				((TextView)view).setHintTextColor(colors);
				break;
			case TEXTCOLORLINK:
				colors = currentTheme.inner ? res.getColorStateList(formattedResID) : getColorStateListWithExtTheme();
				((TextView)view).setLinkTextColor(colors);
				break;
			case TEXTCOLORHIGHLIGHT:
				color = currentTheme.inner ? res.getColor(formattedResID) : getColorWithExtTheme();
				((TextView)view).setHighlightColor(color);
				break;
			case TEXTVIEW_DRAWABLE:
				((TextView)view).setCompoundDrawablesWithIntrinsicBounds(info.drawableLeft, info.drawableTop,
						info.drawableRight, info.drawableBottom);
				break;
			case TEXTVIEW_SHADOW:
				((TextView)view).setShadowLayer(info.radius, info.shadowDx, info.shadowDy, info.shadowColor);
				break;
			}
			return true;
		}catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	private static ColorStateList getColorStateListWithExtTheme() {
		// TODO Auto-generated method stub
		return null;
	}

	private static int getColorWithExtTheme() {
		// TODO Auto-generated method stub
		return 0;
	}

	public static class ViewTheme {
		private final WeakReference<View> viewRef;
		private final Set<ThemeParamInfo> params = new HashSet<ThemeParamInfo>();

		public View getView() {
			return viewRef.get();
		}

		private ViewTheme(View view) {
			viewRef = new WeakReference<View>(view);
		}

		public void addParam(ThemeParam param, int resID) {
			params.add(new ThemeParamInfo(param, resID, getView().getResources()));
		}

		public void ok() {
			addThemableView(this);
		}

		private boolean apply() {
			View view = getView();
			if(view != null) {
				if(Debug.LOG) Log.i(getClass().getSimpleName(), "应用主题--view:" + view);
				Resources res = getView().getResources();
				boolean b = true;
				for(ThemeParamInfo info : params) {
					b &= applyViewParam(view, info, res);
				}
				return b;
			}
			return false;
		}
	}

	private static class ThemeParamInfo {
		final ThemeParam param;
		final int resID;
		/**完整的资源名称："package:type/entry".**/
		final String resName;

		public ThemeParamInfo(ThemeParam param, int resID, Resources res) {
			this.param = param;
			this.resID = resID;
			this.resName = res.getResourceName(resID);
		}

		@Override
		public boolean equals(Object o) {
			//可能多个属性共用一个resId，因此要param也必须相同。比如一个View有多个应用color的属性用到了同一个颜色，则共用同一个resId
			return super.equals(o) || (resID == ((ThemeParamInfo)o).resID && param == ((ThemeParamInfo)o).param);
		}

		@Override
		public int hashCode() {
			return resID;
		}
	}

	/**优先应用当前处于活动状态的context的view的主题**/
	private static Activity currentContext;
	/*package*/ static Theme currentTheme	= Theme.DEFAULT;
	private static WeakHashMap<Activity, List<ViewTheme>> themableViewMap = new WeakHashMap<Activity, List<ViewTheme>>();

	/*private static void onContextCreated(Context context) {
		//应该在程序初始化的时候根据系统配置和设置来加载主题，而不应该在这里加载
		//loadThemes();
	}*/

	/*package*/ static ViewTheme createThemableView(View view) {
		if(view == null) throw new NullPointerException("参数 view 不能为 null");
		return new ViewTheme(view);
	}

	private static void addThemableView(ViewTheme viewTheme) {
		View view = viewTheme.getView();
		if(view != null) {
			currentContext = (Activity)view.getContext();

			List<ViewTheme> list = themableViewMap.get(currentContext);
			if(list == null) {
				list = new LinkedList<ViewTheme>();
				themableViewMap.put(currentContext, list);
			}
			list.add(viewTheme);
		}
	}

	/*package*/ static enum ThemeParam {
		BG,
		BGCOLOR,

		TEXTSIZE,
		TEXTTYPEFACE,

		TEXTCOLOR,
		TEXTCOLORHINT,
		TEXTCOLORLINK,
		TEXTCOLORHIGHLIGHT,

		TEXTVIEW_DRAWABLE,
		TEXTVIEW_DRAWLEFT,
		TEXTVIEW_DRAWTOP,
		TEXTVIEW_DRAWRIGHT,
		TEXTVIEW_DRAWBOTTOM,

		TEXTVIEW_SHADOW,
		TEXTVIEW_SHADOWCOLOR,
		TEXTVIEW_SHADOWDX,
		TEXTVIEW_SHADOWDY,
		TEXTVIEW_SHADOWRADIUS
	}
}
